import json
from channels.generic.websocket import AsyncWebsocketConsumer

class TimetableConsumer(AsyncWebsocketConsumer):
    async def connect(self):
        await self.channel_layer.group_add('timetable', self.channel_name)
        await self.accept()

    async def disconnect(self, close_code):
        await self.channel_layer.group_discard('timetable', self.channel_name)

    async def receive(self, text_data):
        data = json.loads(text_data)
        await self.channel_layer.group_send('timetable', {
            'type': 'broadcast_message',
            'message': data
        })

    async def broadcast_message(self, event):
        await self.send(text_data=json.dumps(event['message']))